//
//  HQMineViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/4.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQMineViewController.h"
#import "AppDelegate.h"
#import "UITabBar+Point.h"

@interface HQMineViewController ()

@end

@implementation HQMineViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _shopName.text = [NSString stringWithFormat:@"店名:%@",[UserModelManager shopName]];
    _userName.text = [UserModelManager userName];
    [_userIcon setImageWithURL:[NSURL URLWithString:[UserModelManager portrait]]];
    [self requestMsgCount];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的";
    
//    if([[UserModelManager position] integerValue] == 1){
        _dataSource = [[NSMutableArray alloc]initWithArray:@[
                                                             @[@{@"itemIcon":@"mine_noti_icon",@"itemName":@"通知",@"itemMark":@"TZ"},
                                                               @{@"itemIcon":@"mine_password_icon",@"itemName":@"修改密码",@"itemMark":@"XGMM"}],
                                                             
                                                             @[
//                                                                 @{@"itemIcon":@"My_staff",@"itemName":@"零售商管理",@"itemMark":@"LSSGL"},
                                                                 //                                                               @{@"itemIcon":@"My_shop",@"itemName":@"店铺统计",@"itemMark":@"DPTJ"},
                                                                 @{@"itemIcon":@"My_custom",@"itemName":@"自定义参数",@"itemMark":@"ZDYCS"}
                                                                 ],
                                                             
                                                             @[@{@"itemIcon":@"mine_about_icon",@"itemName":@"关于我们",@"itemMark":@"GYWM"}],
                                                             @[@{@"itemIcon":@"exit",@"itemName":@"退出登录",@"itemMark":@"TCDL"}]]
                       
                       ];
//    }else{
//        _dataSource = [[NSMutableArray alloc]initWithArray:@[
//                                                             @[@{@"itemIcon":@"mine_noti_icon",@"itemName":@"通知",@"itemMark":@"TZ"},
//                                                               @{@"itemIcon":@"mine_password_icon",@"itemName":@"修改密码",@"itemMark":@"XGMM"}],
//
//                                                             //                                                             @[@{@"itemIcon":@"My_shop",@"itemName":@"解忧杂货店",@"itemMark":@"WDDP"}],
//
//                                                             @[@{@"itemIcon":@"mine_about_icon",@"itemName":@"关于我们",@"itemMark":@"GYWM"}],
//
//                                                             @[@{@"itemIcon":@"exit",@"itemName":@"退出登录",@"itemMark":@"TCDL"}]]];
//    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataSource[section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == _dataSource.count-1){
        static NSString *cellID = @"ExitCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.textLabel.text = @"退出登录";
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = [self colorHexValue:0x233040];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        return cell;
    }else{
        NSDictionary *dict = _dataSource[indexPath.section][indexPath.row];
        if([dict[@"itemMark"] isEqualToString:@"WDDP"]){
            HQMineOtherCell *cell = [HQMineOtherCell cellWithTableView:tableView];
            cell.dataInfo = dict;
            return cell;
        }else{
            HQMineCell *cell = [HQMineCell cellWithTableView:tableView];
            cell.dataInfo = dict;
            if(indexPath.section==0){
                cell.tag = 1000 + indexPath.row;
            }
            return cell;
        }
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dataInfo = _dataSource[indexPath.section][indexPath.row];
    if([dataInfo[@"itemMark"] isEqualToString:@"TZ"]){
        HQNotiViewController *notiVc = [[HQNotiViewController alloc]init];
        notiVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:notiVc animated:YES];
    }else if([dataInfo[@"itemMark"] isEqualToString:@"XGMM"]){
        HQEditPasswordViewController *editPasswordVc = [[HQEditPasswordViewController alloc]init];
        editPasswordVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:editPasswordVc animated:YES];
    }else if([dataInfo[@"itemMark"] isEqualToString:@"GYWM"]){
        HQAboutViewController *aboutVc = [[HQAboutViewController alloc]init];
        aboutVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:aboutVc animated:YES];
    }else if([dataInfo[@"itemMark"] isEqualToString:@"LSSGL"]){
        HQEmployeeManageViewController *employeeManageVc = [[HQEmployeeManageViewController alloc]init];
        employeeManageVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:employeeManageVc animated:YES];
    }else if([dataInfo[@"itemMark"] isEqualToString:@"DPTJ"]){
        HQShopStatisticsViewController *shopStatisticsVc = [[HQShopStatisticsViewController alloc]init];
        shopStatisticsVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:shopStatisticsVc animated:YES];
    }else if([dataInfo[@"itemMark"] isEqualToString:@"ZDYCS"]){
        HQCustomArgumentViewController *customArgumentVc = [[HQCustomArgumentViewController alloc]init];
        customArgumentVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:customArgumentVc animated:YES];
    }else if([dataInfo[@"itemMark"] isEqualToString:@"WDDP"]){
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"您确认退出小美裁缝店吗?" message:nil  preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    }else if([dataInfo[@"itemMark"] isEqualToString:@"TCDL"]){
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要退出登录吗?"  preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [UserModelManager logOut];
            [(AppDelegate *)[UIApplication sharedApplication].delegate showLogin];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {}]];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

/**
 个人信息详情
 
 @param sender 按钮
 */
- (IBAction)userInfoDetail:(id)sender {
    HQUserInfoDetailViewController *userInfoDetailVc = [[HQUserInfoDetailViewController alloc]init];
    userInfoDetailVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:userInfoDetailVc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section == 0){
        return 5;
    }
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return 0;
    }
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

/**
 获取未读消息数量
 */
-(void)requestMsgCount{
//        self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", 8];
    
//    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam getmsgcount:[UserModelManager userID]] withMethod:[HQMethod getmsgcount] suc:^(NSDictionary *sucData) {
//        [self hiddnHud];
        HQMineCell *cell = [_tableView viewWithTag:1000];
        if([sucData[kStatus]integerValue] == 1){
            NSString *msgCount = [NSString stringWithFormat:@"%@",sucData[@"data"][@"msg_unread"]];
            if([msgCount integerValue]>0){
                cell.msgCount.hidden = NO;
                cell.msgCount.text = msgCount;
                //显示
                [self.tabBarController.tabBar showBadgeOnItemIndex:3];
            }else{
                cell.msgCount.hidden = YES;
                //隐藏
                [self.tabBarController.tabBar hideBadgeOnItemIndex:3];
            }
        }else{
            cell.msgCount.hidden = YES;
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

@end
